// pages/index/userCenter/userCenter.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    followOrNot: 0, // 0未关注  1已关注
    travelogueList: [],
    strategyList: [], //获取当前景点对应的攻略
    userId: '' ,//当前页面用户的id
    isFollowed: '' , //当前用户是否被我关注的标识，0未关注，1已关注
    questionList: '',
    userInfo: '',
    fanList: [],
    followedList: []
  },

  // 返回上一页
  back() {
    wx.navigateBack();
  },


  // 关注
  changeFollow() {
    if(this.data.isFollowed==1){
      var id = getApp().globalData.loginfo.userId;
      wx.request({
        url: 'http://localhost:8080/user/followed/deleteMyFollowed',
        method: 'delete',
        header: {
          Authorization: getApp().globalData.authorization
        },
        data: {
          followedId: id, //登录的用户id
          userId: this.data.userId,
        },
        success:(resp)=>{
          //重新查询用户中心列表
        this.getUserInfo()
        this.getQuestionByUserId()
        this.getFans()
        this.getFollowed()
        this.setData({
          isFollowed: 0
        })
        }
  
      })
    }else if(this.data.isFollowed==0) {
      //获取我的id
      var id = getApp().globalData.loginfo.userId;
      wx.request({
        url: 'http://localhost:8080/user/followed/followOther',
        method: 'POST',
        header: {
          Authorization: getApp().globalData.authorization
        },
        data: {
        //因为先查找自己是否被他关注，所以这里的关注人idfollowedId为他的id，userId也就是被关注的人的id写我的id,后面步			骤在业务层已经处理过了
          followedId: this.data.userId,
          userId: id
        },
        success:(resp)=> {
          //重新查询用户中心列表
          this.getUserInfo()
          this.getQuestionByUserId()
          this.getFans()
          this.getFollowed()
          this.setData({
            isFollowed: 1
          })
        }
      })
    }
    
  },
  // 攻略详情     
  getStrategy() {
    wx.request({
      url: `http://localhost:8080/strategy/strategy/list`,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {},
      success: (resp) => {
        this.setData({
          strategyList: resp.data.rows
        });
      },
      complete: (resp) => {
        console.log('strategyList', this.data.strategyList)
      }
    })
  },

  //前往全部攻略
  goAllStrategy() {
    wx.navigateTo({
      url: '/pages/index/userCenter/allStrategy/allStrategy',
    })
  },

  //封装获取该个用户下的所有回答
  getQuestionByUserId() {
    wx.request({
      url: `http://localhost:8080/content/question/user/${this.data.userId}`,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success:(resp)=>{
        this.setData({
          questionList: resp.data.rows
        })
      }
    })
  },


  //获取当前用户关注的用户
  getFollowed() {
    wx.request({
      url: `http://localhost:8080/user/followed/followed/${this.data.userId}`,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success:(resp)=> {
        this.setData({
          followedList: resp.data.rows
        })
      }
    })
  },


  //获取当前用户的粉丝
  getFans() {
    wx.request({
      url: `http://localhost:8080/user/followed/fans/${this.data.userId}`,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success:(resp)=> {
        this.setData({
          fanList: resp.data.rows
        })
      }
    })
  },

  //封装获取当前用户的信息函数
  getUserInfo() {
    wx.request({
      url: `http://localhost:8080/system/user/${this.data.userId}`,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success:(resp)=>{
        this.setData({
          userInfo: resp.data.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userId: options.userId,
      isFollowed: options.isFollowed
    })
    this.getUserInfo()
    this.getQuestionByUserId()
    this.getFans()
    this.getFollowed()
    this.getStrategy()
    wx.request({
      url: 'http://localhost:8080/travelogue/travelogue/list',
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success: (resp) => {
        this.setData({
          travelogueList: resp.data.rows
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})